<?php
class o2o_ctl_admin_server extends desktop_controller {

    var $name = "服务端管理";
    var $workground = "channel_center";

    function index() {
        $title = '服务端管理';
        $this->finder('o2o_mdl_server', array(
            'title' => $title,
            'actions' => array(
                array('label' => '添加服务端', 'href' => 'index.php?app=o2o&ctl=admin_server&act=add&finder_id=' . $_GET['finder_id'], 'target' => '_blank'),
            ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => true,
            'use_buildin_export' => false,
            'use_buildin_import' => false,
            'use_buildin_filter'=>true,
        ));
    }

    /*
     * 添加服务端
     */
    function add() {
        $this->_edit();
    }

    /*
     * 编辑服务端
     */
    function edit($server_id) {
        $this->_edit($server_id);
    }

    function _edit($server_id=NULL) {
        $serverObj = $this->app->model("server");

        $type_list = o2o_conf_server::getTypeList();
        $this->pagedata['type_list'] = $type_list;

        if ($server_id) {
            $server_id = intval($server_id);
            $server = $serverObj->dump($server_id);
            $this->pagedata['server'] = $server;
        }

        $this->pagedata['title'] = '添加/编辑服务端';
        $this->singlepage("admin/system/server.html");
    }

    function save() {
        $serverObj = $this->app->model("server");

        $url = 'index.php?app=o2o&ctl=admin_store&act=index';
        $this->begin($url);
        $save_data = $_POST['server'];

        if (!$save_data['old_server_bn']) {
            $shop_detail = $serverObj->dump(array('server_bn' => $save_data['server_bn']), 'server_bn');
            if ($shop_detail['server_bn']) {
                $this->end(false, '编码已存在，请重新输入');
            }
        }

        $rt = $serverObj->save($save_data);
        $msg = $rt ? '保存成功' : '保存失败';

        $this->end($rt, $msg);
    }
}